import NetUtils from '../../utils/NetUtils.js'

const app = getApp()

Page({
  data: {
    movies: [
      { id: 1, img: "http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg" },
      { id: 2, img: "http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg" },
      { id: 3, img: "http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg" },
    ],
    grids: [
      { text: '违章', image: '../../res/images/home/home_grid_item1.png' },
      { text: '加油', image: '../../res/images/home/home_grid_item2.png' },
      { text: '洗车', image: '../../res/images/home/home_grid_item3.png' },
      { text: '赚钱养车', image: '../../res/images/home/home_grid_item1.png' },
      { text: '粤通卡网点', image: '../../res/images/home/home_grid_item2.png' },
      { text: '车宝周边游', image: '../../res/images/home/home_grid_item3.png' },
      { text: '车牌认证', image: '../../res/images/home/home_grid_item2.png' },
      { text: '更多', image: '../../res/images/home/home_grid_item1.png' },
    ],
    words: [],
    // 上拉加载的flag
    flagLoadmore: false,
    // loading界面显示
    flagLoading: true,
    // 下拉刷新的flag
    flagPull: false,
    // 没有更多
    flagNoMore: false,
    // 页码 【页码，总页码，需要在网络请求中获取，这里只是作为测试使用】
    page: 1,
    // 总页码
    totalPage: 1,

    time: 2000,

    hidden: true,

  },
  onLoad: function () {

  },
  onReady: function () {

    setTimeout(() => {
      this.setData({
        words: [
          { message: 'haha' }, { message: 'haha' }, { message: 'haha' }, { message: 'haha' },
          { message: 'haha' }, { message: 'haha' }, { message: 'haha' }, { message: 'haha' },
          { message: 'haha' }, { message: 'haha' }, { message: 'haha' }
        ],
        flagLoading: false
      })
    }, 5000)
  },
  // 下拉刷新回调接口
  onPullDownRefresh: function () {

    let flagPull = this.data.flagPull

    // 防止爆破下拉【一下子拉了好多次】
    if (flagPull) return

    let words = this.data.words
    let time = this.data.time


    setTimeout(() => {
      for (let i = 0; i < 11; i++) {
        words.unshift({ message: 'world' + i })
      }

      this.setData({
        words: words,
        flagPull: false
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, time)

    this.setData({ flagPull: true })
  },

  onReachBottom: function () {
    let flag = this.data.flagLoadmore
    let page = this.data.page
    let totalPage = this.data.totalPage
    let time = this.data.time

    // 防止重复调用
    if (flag == true) return

    if (page > totalPage) {
      this.setData({ flagNoMore: true })
      return
    }

    let words = this.data.words

    setTimeout(() => {
      page++
      for (let i = 0; i < 11; i++) {
        words.push({ message: 'world' + i })
      }

      this.setData({
        words: words,
        page: page,
        flagLoadmore: false
      })
    }, time)

    this.setData({ flagLoadmore: true })
  },

  onItemClick: function (e) {
    var index = e.currentTarget.dataset.index; //打印可以看到，此处已获取到了对应的id        
    console.log(index);

    wx.navigateTo({
      url: '../toast/toast',
    })


  },

  // 每个swiper item的点击事件
  onSwiperItemClick: function (event) {
    // wx.showToast({
    //   title: event.target.dataset.postid + "",
    // })

    this.setData({ hidden: false })

  },
  onGridItemClick: function (event) {
    let index = event.currentTarget.dataset.index;
    console.log(index)

    wx.navigateTo({
      url: '../webview/webview',
    })
  },

  confirm: function () {
    this.setData({ hidden: true })
  }
});